#include "SelectionManager.h"
#include "DataSelection.h"
#include <assert.h>
#include <memory>

using namespace std;

namespace kryptos { namespace selection {

bool managerInitialized = false;

bool SelectionManager::initialized()
{
    return managerInitialized;
}

SelectionManager::SelectionManager( const char * windowTitle,
                                    unsigned int time_before_paste,
                                    unsigned int time_after_paste,
                                    unsigned int time_between_keystrokes
                                  )
{
    Init(windowTitle, time_before_paste, time_after_paste, time_between_keystrokes);
    managerInitialized = true;
}

SelectionManager& SelectionManager::manager(const std::string& appName)
{
    // if its already initialized then appName is not necesary because it was already set
    assert(managerInitialized ? true : !appName.empty());

    static SelectionManager instance(appName.c_str());
    return instance;
}

SelectionManager::~SelectionManager()
{
}

bool SelectionManager::isDataSelected()
{
    return !m_dataSelection.empty();
}

bool SelectionManager::selectionIsFileSelection()
{
    return m_selectionType == FILE_SELECTION;
}

bool SelectionManager::selectionIsTextSelection()
{
    return m_selectionType == DATA_SELECTION;
}

std::vector<unsigned char> SelectionManager::selectedData()
{
    return m_dataSelection;
}

void SelectionManager::lookForSelection()
{
    char* selection = NULL;
    int selectionSize = 0;

    m_selectionType = GetSelection(selection, selectionSize, 1);

    m_dataSelection.clear();

    if (m_selectionType != -1)
        m_dataSelection.assign(selection, selection+selectionSize);

    FreeSelectionBuffer(selection);
}

int SelectionManager::replaceSelection(const char* data, int usePlainText, bool openEditor, bool dontCheckResult)
{
    return ReplaceSelection(data, usePlainText, openEditor, dontCheckResult);
}
}}
